var express = require('express');
var router = express.Router();
var db = require('../database/db.js')
var session = require('express-session');
var mongo = require('mongodb');
const os = require('os');
const userControl = require('../bin/control/userControl')
const teacher = require('../bin/models/teacherModels');

/* GET users listing. */
router.get('/', function(req, res, next) {  //登录页
  res.render('login');
});
router.get('/admin', function(req, res, next) { //主页框架
  res.render('admin');
});
router.get('/welcome', function(req, res, next) { // 主页
  res.render('welcome',{
    osType:os.type(),
    osUptime:os.uptime()
  });
});


router.get('/userList', function(req, res, next) { // 所有的用户
  var page = parseInt(req.query.page);
  if(!page){
    page = 1;
  }
  userControl.count(function(number){
    userControl.find_num(page,function(dbs){
    res.render('userList',{data:JSON.stringify(dbs),data_h:dbs,number:number,page:page});
  });
 })
});

router.get('/teacher', function(req, res, next) { // 所有的用户
  var page = parseInt(req.query.page);
  if(!page){
    page = 1;
  }
  userControl.count(function(number){
    teacher.find_num({},10,page-1,function(dbs){
    res.render('teacher',{data:JSON.stringify(dbs),data_h:dbs,number:number,page:page});
  });
 })
});

router.get('/teacherAdd', function(req, res, next) { // 所有的教师
 res.render('teacherAdd')
});

router.get('/teacherEditor',async function(req, res, next) { // 查看单一教师
  let _id = req.query.id;
  const result = await teacher.findOneuser(_id)
  console.log(result)
    res.render('teacherEditor',{userData:result,_id:_id});
});

router.get('/userEditor', function(req, res, next) { // 查看单一用户
  let _id = req.query.id;
  userControl.findOneuser(_id,(result)=>{
    res.render('userEditor',{userData:result,_id:_id});
  })
});

module.exports = router;
